package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.task.UserTaskData;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.*;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserTaskDaoImpl extends TempDataObjectDao<UserTaskData> {

	public Map<Integer, UserTaskData> getUserTaskDataMap(long userId) {
		ResultSet rs = null;
		CallableStatement call = null;
		Map<Integer, UserTaskData> dataMap = new ConcurrentHashMap<>();
		String sql = "select * from t_u_task where userId=? and isDelete=0";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserTaskData data = parse(rs);
				dataMap.put(data.getTaskId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	@Override
	protected boolean addData(UserTaskData data) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_u_task(userId,taskId,taskType,state,preTaskId,value,startTime,endTime,finishTime,isDelete) VALUES(?,?,?,?,?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setLong(i++, data.getUserId());
			call.setInt(i++, data.getTaskId());
			call.setInt(i++, data.getTaskType());
			call.setInt(i++, data.getState());
			call.setInt(i++, data.getPreTaskId());
			call.setString(i++, data.getValue().toString());
			call.setTimestamp(i++, new Timestamp(data.getStartTime().getTime()));
			call.setTimestamp(i++, new Timestamp(data.getEndTime().getTime()));
			call.setLong(i++, data.getFinishTime());
			call.setBoolean(i++, data.isDelete());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected boolean updateData(UserTaskData data) {
		PreparedStatement call = null;
		String sql = "UPDATE t_u_task SET taskType=?,state=?,preTaskId=?,value=?,startTime=?,endTime=?,finishTime=?,isDelete=? where userId=? and taskId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setInt(i++, data.getTaskType());
			call.setInt(i++, data.getState());
			call.setInt(i++, data.getPreTaskId());
			call.setString(i++, data.getValue().toString());
			call.setTimestamp(i++, new Timestamp(data.getStartTime().getTime()));
			call.setTimestamp(i++, new Timestamp(data.getEndTime().getTime()));
			call.setLong(i++, data.getFinishTime());
			call.setBoolean(i++, data.isDelete());
			call.setLong(i++, data.getUserId());
			call.setInt(i++, data.getTaskId());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	public boolean deleteUserTaskData(long userId, int taskId) {
		CallableStatement call = null;
		String sql = "delete from t_u_task where userId=? and taskId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			call.setInt(2, taskId);

			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected UserTaskData parseData(ResultSet rs) throws SQLException {
		UserTaskData data = new UserTaskData();
		data.setUserId(rs.getLong("userId"));
		data.setTaskId(rs.getInt("taskId"));
		data.setTaskType(rs.getInt("taskType"));
		data.setState(rs.getInt("state"));
		data.setPreTaskId(rs.getInt("preTaskId"));
		String value = rs.getString("value");
		if(!StringUtils.isNullOrEmpty(value)) {
			data.setValue(new BigInteger(value));
		}
		data.setStartTime(rs.getTimestamp("startTime"));
		data.setEndTime(rs.getTimestamp("endTime"));
		data.setFinishTime(rs.getLong("finishTime"));
		data.setDelete(rs.getBoolean("isDelete"));
		return data;
	}
}
